home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / WackyWindows / Sound / snd.c next >
C/C++ Source or Header  |  1998-06-19  |  4KB  |  150 lines

  1. // ******************************************************************
  2. //    program written by 
  3. //                         Paul Baxter
  4. //                         MacHack'98
  5. //
  6. //
  7. // ******************************************************************
  8.  
  9. #include <A4Stuff.h>
  10. #include <stdlib.h>
  11. #include "patch.h"
  12. // #include "debug.h"
  13.  
  14. // Function Prototypes
  15. pascal void PlaySound(void);
  16. pascal void StopSoundNow(void);
  17. pascal void Sound_CallBack(SndChannelPtr chan, SndCommand *cmd);
  18.  
  19. // Globals
  20. SndListHandle* gMySounds = nil;
  21. SndChannelPtr gSoundChannel = nil;
  22. short gNumSounds = 0;
  23. short *gSoundIndex = nil;
  24. short gCurSound = 0;
  25.  
  26. #ifdef powerc
  27.     // for Metrowerks' linker, this defines the interface for main().
  28.     ProcInfoType __procinfo = SoundInstallerUPPProcInfo;
  29. #endif
  30.  
  31. pascal PlaySoundUPP main(short numsounds, short* soundindex, SndListHandle* sounds);
  32.  
  33. // * ******************************************************************************
  34. // *     main
  35. // *             Entry point for the PlaySound Installer
  36. // * ******************************************************************************
  37. pascal PlaySoundUPP main(short numsounds, short* soundindex, SndListHandle* sounds)
  38. {
  39.     SndCallBackUPP snd_callbackptr;
  40.     PlaySoundUPP SoundPlayer;
  41.     SndCommand    mySndcommand;
  42.     THz theZone;
  43.     short rfile;
  44.     OSErr err;
  45.  
  46.     EnterCodeResource();
  47.  
  48.     // DEBUG("Enter SoundInstaller");
  49.  
  50.     gSoundIndex =soundindex;
  51.     gMySounds = sounds;
  52.     gNumSounds =  numsounds;
  53.  
  54.     theZone = GetZone();
  55.     SetZone(SystemZone());
  56.  
  57.     SoundPlayer = nil;
  58.     srand((unsigned int)TickCount());
  59.  
  60.     rfile = CurResFile();
  61.  
  62.     SoundPlayer = NEW_PLAYSOUNDPROC(PlaySound);
  63.     if (SoundPlayer) {
  64.         snd_callbackptr = NewSndCallBackProc(Sound_CallBack);
  65.         if (snd_callbackptr) {
  66.             err = SndNewChannel(&gSoundChannel, 0, 0, snd_callbackptr);
  67.             if (!err && gSoundChannel) {
  68.                 mySndcommand.cmd = quietCmd;
  69.                 mySndcommand.param2 = 0;
  70.                 err = SndDoImmediate(gSoundChannel, &mySndcommand);
  71.             }
  72.         }
  73.     }
  74.     SetZone(theZone);
  75.  
  76.     // DEBUG("Exit SoundInstaller");
  77.  
  78.     ExitCodeResource();
  79.  
  80.     return SoundPlayer;
  81. }
  82.  
  83. // * ******************************************************************************
  84. // *     PlaySound
  85. // *             Function to Play Sounds
  86. // * ******************************************************************************
  87. pascal void PlaySound(void)
  88. {
  89.     SndCommand    mySndcommand;
  90.     Handle theSoundHandle;
  91.     OSErr err;
  92.     short index;
  93.  
  94.     EnterCodeResource();
  95.  
  96.     if (gSoundChannel && gMySounds && gSoundIndex) {
  97.  
  98.         StopSoundNow();
  99.  
  100.         index = gSoundIndex[gCurSound++];        
  101.         if (gCurSound >= gNumSounds)
  102.             gCurSound = 0;
  103.  
  104.         theSoundHandle = (Handle) gMySounds[index];
  105.         if (theSoundHandle) {
  106.             err = SndPlay(gSoundChannel, (SndListHandle)theSoundHandle, true);
  107.             mySndcommand.cmd = callBackCmd;
  108.             mySndcommand.param2 = (long)theSoundHandle;
  109.             err = SndDoCommand(gSoundChannel, &mySndcommand, true);
  110.  
  111.         }
  112.     }
  113.  
  114.     ExitCodeResource();
  115. }
  116.  
  117. // * ******************************************************************************
  118. // *     Sound_CallBack
  119. // *             Sound Call Back Routine
  120. // * ******************************************************************************
  121. pascal void Sound_CallBack(SndChannelPtr chan, SndCommand *cmd)
  122. {
  123. #pragma unused (chan, cmd)
  124.     EnterCodeResource();
  125.     ExitCodeResource();
  126. }
  127.  
  128. // * ******************************************************************************
  129. // *     StopSoundNow
  130. // *             Stop the current sound
  131. // * ******************************************************************************
  132. pascal void StopSoundNow(void)
  133. {
  134.     SndCommand    mySndcommand;
  135.     OSErr err;
  136.  
  137.     EnterCodeResource();
  138.     if (gSoundChannel) {
  139.         mySndcommand.cmd = quietCmd;
  140.         mySndcommand.param2 = 0;
  141.         err = SndDoImmediate(gSoundChannel, &mySndcommand);
  142.  
  143.         mySndcommand.cmd = callBackCmd;
  144.         mySndcommand.param2 = 0;
  145.         err = SndDoImmediate(gSoundChannel, &mySndcommand);
  146.     }
  147.     ExitCodeResource();
  148. }
  149.  
  150.